package com.jt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.util.OjbectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.JedisCluster;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/19
 */
@Service(timeout = 10000)
public class DubboUserServiceImpl implements DubboUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public void saveUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setEmail(user.getPhone());
        userMapper.insert(user);
    }

    @Override
    public String login(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) {
            return null;
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        user1.setPassword("");
        jedisCluster.setex(uuid, 30*24*60*60, OjbectMapperUtil.toJson(user1));
        return uuid;
    }
}
